﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using Safover.LidarCollector.Core;

namespace Rt.VTH50
{
    public class VirtualVTH50RotaryTableDevice : RotaryTableDevice
    {
        #region Singleton
        public static VirtualVTH50RotaryTableDevice Instance { get; } = new VirtualVTH50RotaryTableDevice();

        private VirtualVTH50RotaryTableDevice()
        {
            HorizontalAngle = originalHorizontalAngle;
            VerticalAngle = originalVerticalAngle;
        }

        #endregion

        bool IsOpen { get; set; }

        bool IsMoving { get; set; }

        public override bool IsSerialPortOpen => IsOpen;

        private const float originalVerticalAngle = 10.0f;
        private const float originalHorizontalAngle = 0f;

        public override string DeviceType => "VirtualVTH50RotaryTableDevice";

        protected override void DoOpenSerialPort()
        {
            IsOpen = true;
        }

        protected override bool DoCloseSerialPort()
        {
            IsOpen = false;
            return true;
        }

        public override bool MoveHorizontallyTo(float angle)
        {
            IsMoving = true;
            InvokeReadingStatusChangedEvent();

            Thread.Sleep(1000);
            HorizontalAngle = angle % 360;

            IsMoving = false;
            InvokeReadingStatusChangedEvent();

            return true;
        }

        public override bool MoveVerticallyTo(float angle)
        {
            IsMoving = true;
            InvokeReadingStatusChangedEvent();

            Thread.Sleep(1000);
            VerticalAngle = angle % 360;

            IsMoving = false;
            InvokeReadingStatusChangedEvent();

            return true;
        }

        public override bool RestoreZero()
        {
            Thread.Sleep(60);

            HorizontalAngle = originalHorizontalAngle;
            VerticalAngle = originalVerticalAngle;

            return true;
        }

        protected override void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            throw new NotImplementedException();
        }

        public override void StopMove()
        {
            Debug.WriteLine($"{DeviceType}: Stopped moving.");
        }

        public override void QueryHorizontalAngle()
        {
            InvokeReadingStatusChangedEvent();
        }

        public override void QueryVerticalAngle()
        {
            InvokeReadingStatusChangedEvent();
        }

        private void InvokeReadingStatusChangedEvent()
        {
            try
            {
                DeviceReadingsUpdated?.Invoke(this, new RotaryTableReadingsUpdatedEventArgs
                {
                    VerticalAngle = VerticalAngle,
                    HorizontalAngle = HorizontalAngle,
                    IsRotaryTableMoving = IsMoving,
                });
            }
            catch { }
        }
    }
}
